package cn.wanda.bi.sunshine.platform.server.service.statistics;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import cn.wanda.bi.sunshine.platform.server.bean.mapper.A06727AppPubHomePageStatMapper;
import cn.wanda.bi.sunshine.platform.server.bean.model.A06727AppPubHomePageStat;
import cn.wanda.bi.sunshine.platform.server.bean.model.A06727AppPubHomePageStatExample;
import cn.wanda.bi.sunshine.platform.server.controller.response.common.ListResponse;

/**
 * Created by wangfupeng1
 */
@Service
public class A06727AppPubHomepageStatService {

    public static final Logger logger = LoggerFactory.getLogger(A06727AppPubHomepageStatService.class);

    @Autowired
    private A06727AppPubHomePageStatMapper a06727AppPubHomePageStatMapper;

    @Cacheable(value="cache:statistics", key = "'cache:statistics:pubhomepagestat:starttime:'+#startTime+':endtime:'+#endTime+':index:'+#index+':limit:'+#limit")
    public ListResponse list(Long dateKeyMax, Long dateKeyMin, Integer index, Integer limit) {
        A06727AppPubHomePageStatExample a06727AppPubHomePageStatExample = new A06727AppPubHomePageStatExample();
        A06727AppPubHomePageStatExample.Criteria criteria = a06727AppPubHomePageStatExample.createCriteria();
        if (dateKeyMax != null && StringUtils.isNotEmpty(dateKeyMax + ""))
            criteria.andDatekeyLessThanOrEqualTo(new Date(dateKeyMax));
        if (dateKeyMin != null && StringUtils.isNotEmpty(dateKeyMin + ""))
            criteria.andDatekeyGreaterThanOrEqualTo(new Date(dateKeyMin));
        int count = a06727AppPubHomePageStatMapper.countByExample(a06727AppPubHomePageStatExample);

        a06727AppPubHomePageStatExample.setOffset(index * limit - limit);
        a06727AppPubHomePageStatExample.setLimit(limit);
        a06727AppPubHomePageStatExample.setOrderByClause("datekey desc");
        List<A06727AppPubHomePageStat> a06727AppPubHomePageStatList = a06727AppPubHomePageStatMapper.selectByExample(a06727AppPubHomePageStatExample);
        return ListResponse.success(a06727AppPubHomePageStatList, count, index, limit);
    }
}
